# coding:utf8

from appserver import db_store
from sqlalchemy import func
import datetime
from datetime import datetime

"""
任务
"""


class Task(db_store.Model):
    __tablename__ = "task"
    __bind_key__ = "main"

    # autoincrement=True,
    id = db_store.Column(db_store.Integer, primary_key=True, comment=u'主键')
    name = db_store.Column(db_store.String(64), nullable=False, comment=u'任务名称')
    task_type = db_store.Column(db_store.String(64), nullable=False, comment=u'任务类型')
    task_tpl = db_store.Column(db_store.String(64), comment=u'任务模板')
    start_time = db_store.Column(db_store.DateTime, nullable=False, comment=u'任务开始时间', default=datetime.now())
    end_time = db_store.Column(db_store.DateTime, nullable=False, comment=u'任务结束时间', default=datetime.now())
    plan_workload = db_store.Column(db_store.Integer, comment=u'计划工作量')
    emergency_degree = db_store.Column(db_store.Integer, comment=u'任务紧急程度')  # 0 特别紧急，1紧急 2，有些紧急 3 等空闲时间处理 4，不用处理
    tool = db_store.Column(db_store.String(64), comment=u'建议佩戴的工具和元器件')
    content = db_store.Column(db_store.String(64), comment=u'工作内容')
    user_id = db_store.Column(db_store.Integer, db_store.ForeignKey('users.id'), comment=u'接单人')
    device_id = db_store.Column(db_store.Integer, db_store.ForeignKey('devices.id'), comment=u"设备id")

    def __repr__(self):
        return "<PartrolTask %s>" % self.name

    def get_max_id(self):
        val = db_store.session.query(func.max(Task.id)).scalar()
        if not val:
            return 0
        else:
            return val

    def save(self):

        if self.id is None:
            self.id = self.get_max_id() + 1
        try:
            db_store.session.add(self)
            db_store.session.commit()
            return True
        except Exception as e:
            print("error is ")
            print(e)
            db_store.session.rollback()
            return False

    @staticmethod
    def get_task_by_id(id_num):
        return Task.query.filter_by(id=id_num).first()

    @staticmethod
    def get_task_by_name(name):
        return Task.query.filter_by(name=name).all()

    @staticmethod
    def get_task_all():
        # 以python列表的形式返回所有任务实例
        return Task.query.all()

    # 以python字典的形式返回任务实例的字典
    def get_dic(self):
        rdic = {}
        rdic['id'] = self.id
        rdic['name'] = self.name
        rdic['task_type'] = self.task_type
        rdic['task_tpl'] = self.task_tpl
        rdic['start_time'] = self.start_time.strftime("%Y-%m-%d %H:%M:%S")
        rdic['end_time'] = self.end_time.strftime("%Y-%m-%d %H:%M:%S")
        rdic['plan_workload'] = self.plan_workload
        rdic['emergency_degree'] = self.emergency_degree
        rdic['tool'] = self.tool
        rdic['content'] = self.content
        rdic['user_id'] = self.user_id
        rdic['device_id']=self.device_id
        return rdic

    def delete(self):
        try:
            db_store.session.delete(self)
            db_store.session.commit()
            return True
        except Exception as e:
            print(e)
            db_store.session.rollback()
            return False